var app = getApp();
import {
  api,
  util,
  dialog
} from '../../../../js/util';
import {
  router
} from '../../../../js/router';
import {
  group
} from '../../../../js/modules/market';
import {
  list
} from "../../../../js/modules/mall";
import BasePage from '../../../../js/common/base-page'
import { hideTabbar } from '../../../../js/compatible';
BasePage({
  pageKey: "",
  /**
   * 页面的初始数据
   */
  data: {
    Flag: 1, //1拼团2预售
    PageIndex: 1,
    PageSize: 20,
    imgDomain: api.imgDomain,
    enableOss: api.enableOss,
    sortField: "Id",
    sortDirect: "desc",
    hasMoreData: true,
    GroupStatus: 1, //状态 1正在进行 0即将开始
    tablist: ['正在进行', '即将开始'],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 35,
    slidewidth: 0,
    goodsList: [],
    timer: null,
    priceDigits: 2,
    windowWidth: 0,
    Currency:'',//货币符号
    isIphoneX:app.globalData.isIphoneX,
    tabindex:-1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    //初始化底部导航
    app.initTabBar();
    var that = this;
    that.setData({
      Flag: options.flag
    });
    this.pageKey = options.flag == 1 ? 'groupList' : 'presellList';
    wx.setNavigationBarTitle({
      title: (options.flag == 2 ? "预售" : "拼团") + "列表"
    });
    this.getWholesaleConfig();
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          slidewidth: (res.windowWidth / that.data.tablist.length) - 80,
          windowWidth: res.windowWidth
        });
      }
    });
  },
  /**加载商品类的配置项参数 */
  getWholesaleConfig() {
    var that = this;
    var fkid = app.dns.proprietorId;
    var fkflag = app.dns.proprietor;
    list.getWholesaleConfigs(
      fkid,
      fkflag,
      function(content) {
        that.setData({
          quantityDigits: content.QuantityDigits,
          priceDigits: content.PriceDigits,
        });
      });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    //隐藏原生底部导航
    hideTabbar()
    var tabindex = app.getTabBarindex();
    this.setData({
      tabindex:tabindex,
      PageIndex: 1,
      Currency:api.Currency || '￥'
    });
    var that = this;
    this.loadGroupProductList(()=>{
      if (that.data.goodsList.length <= 0) {
        var offsetLeft = Math.ceil(that.data.windowWidth / that.data.tablist.length - 20 * that.data.windowWidth / 750);
        that.setData({
          sliderOffset: offsetLeft,
          activeIndex: 1,
          GroupStatus: 0,
        });
        that.loadGroupProductList();
      }
    });


  },
  
  /**分页加载拼团商品列表
   * GroupStatus：活动进行状态 1正在进行 0即将开始
   * Flag ：1拼团2预售3限时特价
   */
  loadGroupProductList(callback) {
    var that = this;
    var param = {
      PageIndex: that.data.PageIndex,
      PageSize: that.data.PageSize,
      sortField: that.data.sortField,
      sortDirect: that.data.sortDirect,
      GroupStatus: that.data.GroupStatus,
      Flag: that.data.Flag
    };
    clearInterval(this.data.timer);
    this.data.timer = null;
    group.loadGroupProductList(param, function(res) {
      var contentlistTem = that.data.goodsList;
      if (that.data.PageIndex == 1) {
        contentlistTem = [];
      }
      if (res.Content.TotalCount > 0) {
        var contentlist = res.Content.Data;
        var goodlist = contentlistTem.concat(contentlist);
        if (goodlist.length < that.data.PageSize) {
          that.setData({
            goodsList: goodlist,
            hasMoreData: false
          });
        } else {
          that.setData({
            goodsList: goodlist,
            hasMoreData: true,
            PageIndex: that.data.PageIndex + 1
          });
        }
        group.setCountDown(that);
      } else {
        that.setData({
          goodsList: [],
          hasMoreData: true,
          PageIndex: 1
        });
      }
      if (callback && typeof (callback)=="function"){
        callback();
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh();
    this.data.PageIndex = this.data.PageIndex + 1;
    this.loadGroupProductList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.hasMoreData) {
      this.loadGroupProductList();
    }
  },
  onGroupNavigator: function(e) {
    const id = e.currentTarget.dataset["id"];
    const productId = e.currentTarget.dataset["productid"];
    const groupStatus = e.currentTarget.dataset["groupstatus"];
    const actualover = e.currentTarget.dataset["actualover"];
    if (actualover == 1 && groupStatus == 2) {
      util.alert("商品已被抢光啦！");
      return;
    }
    router.navigate({
      path: "groupDetail",
      params: {
        ptype: this.data.Flag,
        id: id,
        productId: productId
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * tab切换
   */
  changeCurrent: function(res) {
    var activeIndex = res.detail.currentNum.id;
    var groupStatus = activeIndex == 0 ? 1 : 0;
    this.setData({
      sliderOffset: res.detail.currentNum.offsetLeft,
      activeIndex: res.detail.currentNum.id,
      GroupStatus: groupStatus,
      PageIndex: 1
    });
    this.loadGroupProductList();
  },
})